﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DHQGHN.DAL.Base;
using DHQGHN.DAL.Common;
using DHQGHN.DAL.Models.Admin;

namespace DHQGHN.DAL.DAO.Admin
{
    public class ApplicationDAO:SqlDAO
    {
        public const string SP_GET_LIST_PAGING_APPLICATION = "spGetListPagingApplication";
        public const string SP_GET_ALL_APPLICATION = "spGetAllApplication";
        public const string SP_GET_LIST_PAGING_APPLICATION_BY_ID = "spGetApplicationById";
        public const string SP_CREATE_OR_UPDATE_APPLICATION = "spCreateOrUpdateApplication";
        public const string SP_DELETE_APPLICATION = "spDeleteApplication";
        public const string SP_CHECK_PERMIT_TO_APPLICATION = "spCheckPermitToApplication";//"spCheckPermitToApp";
        public const string SP_CHECK_EXIST_APPLICATION = "spCheckExistApp";
        public const string SP_GET_APP_NAME_BY_USER_ID = "spGetAppNameByUserId";
        public const string SP_GET_APP_NAME_BY_GROUP_ID = "spGetAppNameByGroupId";


        public const string Param_AppId = "@AppId";
        public const string Param_TableId = "@TableId";
        public const string Param_AppName = "@AppName";
        public const string Param_AppCode = "@AppCode";
        public const string Param_UserId = "@UserId";
        public const string Param_GroupId = "@GroupId";
        //public const string Param_RecordCount = "@RecordCount";
        public ApplicationDAO(String connectionString, System.Type type = null)
            : base(connectionString, type)
        {

        }

        internal DataPage<ApplicationModel> GetDynamicListPaging(ApplicationModel model, int pageSize, int pageIndex, Dictionary<String, String> mapDictionary)
        {
            try
            {
                //SqlCommand command = SharedSqlDao.GetSprocCommand(pr_Person_GetAll);
                SqlCommand command = this.GetSprocCommand(SP_GET_LIST_PAGING_APPLICATION);
                if (model != null)
                {
                    command.Parameters.Add(CreateParameter(Param_AppName, "%" + model.AppName + "%"));
                    command.Parameters.Add(CreateParameter(Param_AppCode, "%" + model.AppCode + "%"));
                }
                command.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
                command.Parameters.Add(new SqlParameter("@PageSize", pageSize));
                return GetDataPage<ApplicationModel>(command, pageIndex, pageSize, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        internal ApplicationModel GetById(int? appId, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_GET_LIST_PAGING_APPLICATION_BY_ID);
                command.Parameters.Add(new SqlParameter(Param_AppId, appId));

                return GetSingle<ApplicationModel>(command, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        internal ApplicationModel GetByCode(string code, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand("spApplication_GetByCode");
                command.Parameters.Add(new SqlParameter("@AppCode", code));

                return GetSingle<ApplicationModel>(command, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        internal bool createOrUpdateApp(ApplicationModel appModel)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_CREATE_OR_UPDATE_APPLICATION);
                command.Parameters.Add(new SqlParameter("@" + ApplicationModel.FIELD_ID, appModel.AppId));
                command.Parameters.Add(new SqlParameter("@" + ApplicationModel.FIELD_APP_NAME, appModel.AppName));
                command.Parameters.Add(new SqlParameter("@" + ApplicationModel.FIELD_APP_CODE, appModel.AppCode));
                command.Parameters.Add(new SqlParameter("@" + ApplicationModel.FIELD_APP_DESCRIPTION, appModel.AppDescription));
                command.Parameters.Add(new SqlParameter("@" + ApplicationModel.FIELD_IS_ACTIVE, appModel.IsActive));
                return ExecuteQuery(command) > 0 ? true : false;
            }
            catch
            {
                throw;
            }
        }

        internal bool deleteApp(int[] appId)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_DELETE_APPLICATION);
                command.Parameters.Add(new SqlParameter(Param_TableId, GetTableId<int>(appId)));

                return ExecuteQuery(command) > 0 ? true : false;
            }
            catch
            {
                throw;
            }
        }

        internal bool checkPermissionOfUser(int userId, string appCode)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_CHECK_PERMIT_TO_APPLICATION);
                command.Parameters.Add(new SqlParameter(Param_AppCode, appCode));
                command.Parameters.Add(new SqlParameter(Param_UserId, userId));
                //command.Parameters.Add(new SqlParameter(Param_RecordCount, null));
                var returnParameter = command.Parameters.Add("@ReturnVal", System.Data.SqlDbType.Int);
                returnParameter.Direction = System.Data.ParameterDirection.ReturnValue;
                ExecuteNonQuery(command);
                var result = returnParameter.Value;
                return Convert.ToInt32(result) > 0 ? true : false;
            }
            catch
            {
                throw;
            }
        }

        internal List<ApplicationModel> GetAll(Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_GET_ALL_APPLICATION);
                return GetList<ApplicationModel>(command, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        internal bool checkAppExist(int appId, string appCode, string appName)
        {
            SqlCommand command = this.GetSprocCommand(SP_CHECK_EXIST_APPLICATION);
            command.Parameters.Add(new SqlParameter(Param_AppCode, appCode));
            command.Parameters.Add(new SqlParameter(Param_AppId, appId));
            command.Parameters.Add(new SqlParameter(Param_AppName, appName));
            //command.Parameters.Add(new SqlParameter(Param_RecordCount, null));
            var returnParameter = command.Parameters.Add("@ReturnVal", System.Data.SqlDbType.Int);
            returnParameter.Direction = System.Data.ParameterDirection.ReturnValue;
            ExecuteNonQuery(command);
            var result = returnParameter.Value;
            return Convert.ToInt32(result) > 0 ? true : false;
        }

        internal List<ApplicationModel> GetAppNameByUserId(int userId, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_GET_APP_NAME_BY_USER_ID);
                command.Parameters.Add(new SqlParameter(Param_UserId, userId));
                return GetList<ApplicationModel>(command, mapDictionary);
            }
            catch
            {
                throw;
            }
        }


        internal List<ApplicationModel> GetAppNameByGroupId(int? groupId, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_GET_APP_NAME_BY_GROUP_ID);
                command.Parameters.Add(new SqlParameter(Param_GroupId, groupId));
                return GetList<ApplicationModel>(command, mapDictionary);
            }
            catch
            {
                throw;
            }
        }
    }
}
